#include "stdafx.h"
#include "util.h"

namespace shared
{
    void QString::SplitString(const std::string &str, const std::string &seperator, std::list<std::string> &array)
    {
        array.clear();
        if (str.empty() || seperator.empty())
            return;

        size_t position;
        std::string tmp_str = str;

        position = tmp_str.find(seperator.c_str());
        while (position != tmp_str.npos)
        {
            if (position)
                array.push_back(tmp_str.substr(0, position));
            tmp_str.erase(0, position + seperator.length());
            position = tmp_str.find(seperator);
        }

        if (!tmp_str.empty())
            array.push_back(tmp_str);
    }

    void QString::SplitString(const std::wstring &str, const std::wstring &seperator, std::list<std::wstring> &array)
    {
        array.clear();
        if (str.empty() || seperator.empty())
            return;

        size_t position;
        std::wstring tmp_str = str;

        position = tmp_str.find(seperator.c_str());
        while (position != tmp_str.npos)
        {
            if (position)
                array.push_back(tmp_str.substr(0, position));
            tmp_str.erase(0, position + seperator.length());
            position = tmp_str.find(seperator);
        }

        if (!tmp_str.empty())
            array.push_back(tmp_str);
    }

    std::string QString::StringReverse(const std::string &input)
    {
        int len = input.size();
        std::string output;
        output.resize(len);
        for (int i = 0; i < len; i++)
        {
            output[i] = input[len - 1 - i];
        }

        return output;
    }
}